<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Cache;
use app\admin\model\User as UserModel;

class Login extends Controller
{   
    public function upload(){
        $file = request()->file('image');
        return $this->uploads($file,'head');
    }
    public function uploads($file,$path){
        $return=[];
            // 获取表单上传文件 例如上传了001.jpg
            // $file = request()->file('image');
            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                // $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'admin');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'. DS . 'user'.DS.$path);
                if($info){
                    // 成功上传后 获取上传信息
                    $return['code']=0;
                    $return['msg']="上传成功";
                    $return['data']='/uploads/'.$path.'/'.str_replace('\\','/',$info->getSaveName());
                }else{
                    // 上传失败获取错误信息
                    $return['code']=1;
                    // $return['msg']="上传成功";
                    $return['msg']="上传失败";
                    $return['data']=$file->getError();
                }
            }
        return json_encode($return,320);
    }
   
    public static function curl_post($url, $data)
    {
        $data  = json_encode($data);
        $headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);
    }
    public static function curl_get($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        $SSL = substr($url, 0, 8) == "https://" ? true : false;
        if ($SSL) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名
        }
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
  
    //保存图片到本地
    public function put_img($uid=1,$path='https://thirdwx.qlogo.cn/mmopen/vi_32/WYnPs44goxGzO3RticNyNdDjQoDnKm3f7NVDNIDGF2s5ytiaUv4D1W12h95pLlqCnaX3YYtpqTX4K45LJFZvz8Zw/132'){
        $imgDir = ROOT_PATH . 'public' . DS . 'uploads'.DS.'user'.DS."head".DS.date('Ymd',time()).DS;
        $head_dir='/uploads/user/head/'.date('Ymd',time()).'/';
        if(!file_exists($imgDir)){
          mkdir($imgDir);
        }
        $res = $this->getImage($path,$imgDir,$filename=md5(rand(0,9999).time()).".jpg",1);
        if($res['file_name']){
            UserModel::where('id',$uid)->update(['head'=>$head_dir.$res['file_name']]);
        }
        // return $res;
    }
   
}